/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/
 
import fever.data.validators.Validator;
import fever.events.ValidationResultEvent;

/**
 * Validates that the length of a String is within a specified range.<br />
 * Can validates if tested string contain defined substring too.
 * 
 * @see fever.data.validators.Validator
 * 
 * @author Romain Ecarnot
 */
class fever.data.validators.StringValidator extends Validator
{
	
	//-------------------------------------------------------------------------
	// Public properties
	//-------------------------------------------------------------------------
	
	/** Substring contained for a valid String. */
	public var content : String;
	
	/** Maximum length for a valid String. */
	public var maxLength : Number;
	
	/** Minimum length for a valid String. */
	public var minLength : Number;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	public function StringValidator()
	{
		super();
		
		content = null;
		maxLength = Number.NaN;
		minLength = Number.NaN;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _doValidation(object) : ValidationResultEvent
	{
		var toValid : String = _buildValidationTarget( object );
		
		if( !_testRequired( toValid ) )
		{	
			_fireEventType( ValidationResultEvent.INVALID );
			return _event;
		}
		
		if( !_testMinLength( toValid ) )
		{	
			_fireEventType( ValidationResultEvent.INVALID );
			return _event;
		}
		
		if( !_testMaxLength( toValid ) )
		{	
			_fireEventType( ValidationResultEvent.INVALID );
			return _event;
		}
		
		if( !_testContent( toValid ) )
		{	
			_fireEventType( ValidationResultEvent.INVALID );
			return _event;
		}
		
		_prepareEvent( ValidationResultEvent.VALID, toValid, "");
		_fireEventType( ValidationResultEvent.VALID );
		
		return _event;
	}
	
	private function _testMaxLength(s : String) : Boolean
	{
		if( !isNaN(maxLength) && s.length > maxLength)
		{
			_prepareEvent( ValidationResultEvent.INVALID, s,_resources.string_too_long_error + " [" + maxLength + "]" );
			return false;
		}	
		return true;
	}
	
	private function _testMinLength(s : String) : Boolean
	{
		if( !isNaN(minLength) && s.length < minLength)
		{
			_prepareEvent( ValidationResultEvent.INVALID, s,  _resources.string_too_short_error + " [" + minLength + "]" );
			return false;
		}	
		return true;
	}
	
	private function _testContent(s : String) : Boolean
	{
		if( content != undefined && s.indexOf(content) < 0)
		{
			_prepareEvent( ValidationResultEvent.INVALID, s, _resources.string_not_contain_error + " ['" + content + "']" );
			return false;
		}	
		return true;
	}
	
}